<template>
    <el-card class="w-full pt-[5px] mt-[10px]">
        <div class="mb-[20px]">
            <el-button @click="historyVisible = true">
                历史申报
            </el-button>
        </div>
        <el-table :data="tableData" class="w-full" @selection-change="handleSelectionChange" stripe>
            <el-table-column type="selection" width="55" />
            <el-table-column prop="name" label="申报计划名称">
                <template #default="scope">
                    <el-link type="primary" @click="viewItem(scope.row)">{{ scope.row.name }}</el-link>
                </template>
            </el-table-column>
            <el-table-column prop="honorName" label="获奖名称" />
            <el-table-column prop="beginDate" label="申报开始时间" />
            <el-table-column prop="endDate" label="申报结束时间" />
            <el-table-column prop="applyState" label="申报状态">
                <template #default="scope">
                    <Feedback :id="scope.row.applyState" :list="sbztList">
                    </Feedback>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="360" align="center">
                <template #default="scope">
                    <el-button type="primary" size="small"
                        v-if="scope.row.applyState == '1' && scope.row.honorApplyBookId == null" plain
                        @click="handleEdit(scope.row)">
                        申报
                    </el-button>
                    <!-- <el-button type="primary" size="small" plain @click="handleEdit(scope.row)">
                        编辑
                    </el-button> -->
                    <el-button type="primary" size="small" plain v-if="scope.row.honorApplyBookId"
                        @click="goSbcl(scope.row)">
                        查看申报
                    </el-button>
                    <el-button type="primary" size="small"
                        v-if="scope.row.applyState == '1' && scope.row.honorApplyBookId && (scope.row.honorApplyBookStatus == '-1' || scope.row.honorApplyBookStatus == '3')"
                        plain @click="handleEdit(scope.row)">
                        申报编辑
                    </el-button>
                    <el-button type="danger" size="small"
                        v-if="scope.row.applyState == '1' && scope.row.honorApplyBookId && scope.row.honorApplyBookStatus == '1'"
                        @click="revoke(scope.row)" plain>
                        撤销申报
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="mt-[20px] flex justify-end">
            <pagination v-show="total > 0" :total="total" v-model:page="pageInfo.pageNum"
                v-model:limit="pageInfo.pageSize" @pagination="getList" />
        </div>
    </el-card>
    <detailEdit v-model="editModelVisible" ref="EditModalRef" :hjztList="hjztList" :hjjbList="hjjbList"
        :zcgsdwList="zcgsdwList" :sydwList="sydwList" :kjxkflList="kjxkflList" :xlList="xlList" :zcList="zcList"
        @callBack="callBackFun" :planInfo="planInfo" />
    <detailView v-model="detailViewdialogVisible" ref="detailviewModalRef" :hjztList="hjztList" :hjjbList="hjjbList"
        :zcgsdwList="zcgsdwList" :sydwList="sydwList" :kjxkflList="kjxkflList" :xlList="xlList" :zcList="zcList"
        :processInstanceId="rowprocessInstanceId" />
    <viewModal v-model="ViewdialogVisible" ref="viewModalRef" :hjjbList="hjjbList" />
    <historyModal v-model="historyVisible" />
</template>

<script setup>
import { onMounted } from 'vue';
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { gethonorApplyInfo, getreviewSchemeList, gethonorApplyBookInfo, gethonorApplyList } from "@/api/achievement/cghj"
import { honorApplyInfoPersonnelList, revokeHonorApplyBook } from "@/api/achievement/staff"
import detailEdit from "./detailEdit.vue";
import detailView from "./detailView.vue";
import historyModal from "./historyModal.vue";
import viewModal from "./viewModal.vue";
import useCategoryStore from "@/store/modules/category";
import { cancelProcess } from "@/api/project/common";
import Cookies from 'js-cookie'
import useUserStore from "@/store/modules/user";
import { getPersonDetails } from "@/api/achievement/common.js"

const categoryStore = useCategoryStore();
const { sbztList, hjjbList, hjztList, zcgsdwList, kjxkflList, xlList, zcList,
    sydwList } = storeToRefs(categoryStore); // 解构状态
const { getsbztList, gethjjbList, gethjztList, getzcgsdwList, getkjxkflList, getxlList, getzcList,
    getsydwList } = categoryStore; // 解构方法

const editModelVisible = ref(false);

const ViewdialogVisible = ref(false);
const detailViewdialogVisible = ref(false);
const historyVisible = ref(false);

const searchForm = ref({
    name: "",
    honorName: "",
    beginDateStart: "",
    beginDateEnd: "",
    endDateStart: "",
    endDateEnd: "",
    honorLevel: "",
    isAutoEnd: "",
    isReview: "",
    applyState: "",
});
const pageInfo = ref({
    pageNum: 1,
    pageSize: 10
})
const total = ref(0)
const tableData = ref([]);

const EditModalRef = ref(null)
const planInfo = ref({
    id: '',
    honorName: '',
    honorLevel: '',
})
const userStore = useUserStore();

//点击编辑
const handleEdit = (i) => {
    planInfo.value.id = i.id
    planInfo.value.honorName = i.honorName
    planInfo.value.honorLevel = i.honorLevel
    if (i.honorApplyBookId) {
        gethonorApplyBookInfo(i.honorApplyBookId).then(res => {
            if (res.code == 200) {
                editModelVisible.value = true;
                EditModalRef.value.getRuleForm(res.data)
            }
        })
    } else {
        editModelVisible.value = true;
        if (Cookies.get("role-key") == 'biz_kyry') {
            let kyryInfo = {
                authorType: '1',
                eduLevelId: '',
                authorName: '',
                isTxAuthor: '0',
                titleId: '',
                authorUnit: '',
                authorUnitId: '',
                workRatio: null,
                personId: '',   //userId
                orderId: 1,
                authorAccount: "",   //职工号
                sexId: '',
                subjectId: "",
                unitId: '',
                unitName: '',
                disable: true
            }
            let pamrs = {
                account: userStore.account
            }
            let kyryInfoArr = []
            getPersonDetails(pamrs).then(res => {
                if (res.code == 200) {
                    kyryInfoArr = res.data.filter((item) => {
                        return item.name == userStore.nickName
                    });
                    if (kyryInfoArr && kyryInfoArr.length) {
                        kyryInfo.authorName = kyryInfoArr[0].name
                        kyryInfo.personId = kyryInfoArr[0].id
                        kyryInfo.authorAccount = kyryInfoArr[0].account
                        kyryInfo.sexId = kyryInfoArr[0].sexId
                        kyryInfo.subjectId = kyryInfoArr[0].subjectId
                        kyryInfo.authorUnitId = kyryInfoArr[0].unitId
                        kyryInfo.authorUnit = kyryInfoArr[0].unitName
                        kyryInfo.eduLevelId = kyryInfoArr[0].eduLevelId
                        kyryInfo.titleId = kyryInfoArr[0].titleId
                        EditModalRef.value.getKyryInfo([kyryInfo])
                    }
                }
            })
        }
    }

};

const detailviewModalRef = ref(null)

const rowprocessInstanceId = ref(null)
const goSbcl = (i) => {
    gethonorApplyBookInfo(i.honorApplyBookId).then(res => {
        if (res.code == 200) {
            rowprocessInstanceId.value = i.processInstanceId
            detailViewdialogVisible.value = true;
            detailviewModalRef.value.getRuleForm(res.data)
        }
    })
}

const viewModalRef = ref(null)
const viewItem = (i) => {
    gethonorApplyInfo(i.id).then(res => {
        if (res.code == 200) {
            ViewdialogVisible.value = true;
            viewModalRef.value.getRuleForm(res.data)
        }
    })
};
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}

const revoke = async (i) => {
    let pamrs = { id: i.honorApplyBookId, checkstatus: '-1' }
    revokeHonorApplyBook(pamrs).then(res => {
        if (res.code == 200) {
            ElMessage.success("撤销成功")
            getList()
        }
    })
    if (i.processInstanceId) {
        let processId = i.processInstanceId
        const res = await cancelProcess(processId);
    }

}

const initList = () => {
    getList()
}
const getList = async (i) => {
    if (i == 1) {
        pageInfo.value.pageNum = 1
    }
    let parms = {
        ...searchForm.value,
        ...pageInfo.value
    }
    let res = await gethonorApplyList(parms)
    if (res.code == 200) {
        tableData.value = res.rows
        total.value = res.total
        console.log(tableData.value)
    }
}

const callBackFun = () => {
    getList()
}
onMounted(() => {
    if (!sbztList.value.length) {
        getsbztList()
    }
    if (!hjjbList.value.length) {
        gethjjbList()
    }
    if (!hjztList.value.length) {
        gethjztList()
    }
    if (!zcgsdwList.value.length) {
        getzcgsdwList()
    }
    if (!kjxkflList.value.length) {
        getkjxkflList()
    }
    if (!xlList.value.length) {
        getxlList()
    }
    if (!zcList.value.length) {
        getzcList()
    }
    if (!sydwList.value.length) {
        getsydwList();
    }

    initList()
});
</script>

<style scoped lang="scss"></style>